package cn.itsource.smartsystem.controller;

import cn.itsource.smartsystem.domain.Student;
import cn.itsource.smartsystem.domain.Teacher;
import cn.itsource.smartsystem.service.StuService;
import cn.itsource.smartsystem.util.AjaxResult;
import cn.itsource.smartsystem.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/stu")
public class StuController {
    @Autowired
    private StuService ss;
    @GetMapping
    public AjaxResult getAll(){
        List<Student> all = ss.getAll();
        ss.getAll();
        return AjaxResult
                .me()
                .setSuccess(true)
                .setMessage("查询成功")
                .setResultObj(all);
    }
   @PutMapping
    public AjaxResult addStudent(@RequestBody Student stu){
       Integer i;
       if(stu.getId() != null){
           i= ss.updateByPrimaryKeySelective(stu);
       }else {
           i = ss.insertStudent(stu);
       }
       if(i>0){
           return AjaxResult
                   .me()
                   .setSuccess(true)
                   .setMessage("操作成功")
                   .setResultObj(i);}
       return null;
   }
    @DeleteMapping("/{id}")
    public AjaxResult delStudent(@PathVariable("id") Long id){
        Integer i = ss.deleteByPrimaryKey(id);
        if(i>0){
            return AjaxResult
                    .me()
                    .setSuccess(true)
                    .setMessage("删除成功")
                    .setResultObj(i);
        }
        return null;
    }
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Student> list = ss.getAll();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-学生信息", "学生信息表", Student.class, "Students", response);
    }
    /**
     * @Description: 老师信息导入模板
     * @Author: Neuronet
     * @Date: 2023/9/25 16:49
     * @param response: 响应对象
     * @Return:
     **/
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<Student> list = new ArrayList<>();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-学生信息", "学生信息表", Student.class, "学生信息导入模板", response);
    }
    /**
     * @Description: 老师信息导入
     * @Author: Neuronet
     * @Date: 2023/9/25 17:35
     * @param file: 要导入的文件
     * @Return:
     **/
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {
        ss.importExcel(file);
        return AjaxResult.me().setSuccess(true).setMessage("导入成功");
    }
}

